﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
using System.Data;
using System.Text;

namespace lcs.Web.supply
{
    public partial class ListWarnInfoSlife : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                string user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                Bind(user_id);
            }
        }

        private void Bind(string uid)
        {
            Maticsoft.BLL.warnInfo wm = new Maticsoft.BLL.warnInfo();

            DataSet ds = wm.GetListVmaterialWarnInfo("");

            DataTable dt = ds.Tables[0];

            DataTable dt_source = dt.Clone();

            dt_source.Columns.Add("warnStr", typeof(string));

            foreach (DataRow row in dt.Rows)
            {
                string material = row["material"].ToString();
                string userId = row["uid"].ToString();

                DataRow newRow = dt_source.NewRow();

                newRow["uid"] = row["uid"];
                newRow["material"] = row["material"];
                newRow["user_name"] = row["user_name"];
                newRow["minNums"] = row["minNums"];
                newRow["maxNums"] = row["maxNums"];
                newRow["warnTime"] = row["warnTime"];
                newRow["intime"] = row["intime"];

                dt_source.Rows.Add(newRow);

            }

            foreach (DataRow row in dt_source.Rows)
            {
                DateTime time = DateTime.Parse(row["intime"].ToString());//入库时间
                decimal minNums = decimal.Parse(row["minNums"].ToString());//最小数量
                decimal maxNums = decimal.Parse(row["maxNums"].ToString());//最大数量
                int warnTime = int.Parse(row["warnTime"].ToString());//报警时间

                string strMsg = "";
               
                if (time.AddMonths(warnTime) < DateTime.Now)
                {
                    strMsg += " 使用寿命过期 ";
                }
                row["warnStr"] = strMsg;
            }

            DataTable dt_sourceFilter = dt_source.Clone();

            foreach (DataRow row in dt_source.Rows)
            {
                if (!string.IsNullOrEmpty(row["warnStr"].ToString()))
                {
                    dt_sourceFilter.ImportRow(row);
                }
            }

            this.GridView1.DataSource = dt_sourceFilter;
            this.GridView1.DataBind();
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                string user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                Bind(user_id);
            }

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=wuliaoWARN.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>物料库存预警</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                string user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                Bind(user_id);
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }
    }
}